#ifndef FILECLIENT_H
#define FILECLIENT_H

#include <QObject>
#include <QAbstractSocket>
#include <QTcpSocket>
#include <QByteArray>
#include <QString>
#include <QStringList>
#include <QFile>

//文件上传下载字段定义
#define FILE_UPLOAD_HEADER             100
#define FILE_UPLOAD_POS                101
#define FILE_UPLOAD_TAIL               103
#define FILE_DOWNLOAD_HEADER           200
#define FILE_DOWNLOAD_CONTENT          202
#define FILE_DOWNLOAD_TAIL             203

//错误码定义
#define OK                             0
#define FILE_WRITE_ERROR               -1
#define FILE_OPEN_ERROR                -2
#define FILE_SEEK_ERROR                -3
#define FILE_ALREADY_EXISTS_ERROR      -4
#define RECV_DATA_TIMEOUT_ERROR        -5
#define RECV_UNKNOW_DATA_ERROR         -6
#define CONNECT_SERVER_ERROR           -7
#define UPLOAD_FILE_ERROR              -8
#define DOWNLOAD_FILE_ERROR            -10

class FileClient : public QObject
{
    Q_OBJECT
public:
    explicit FileClient(QString ip,quint16 port,QObject *parent = nullptr);
    ~FileClient();
signals:

public slots:
    //文件上传
    qint64 filesUpload(QStringList filepath_list);
    qint64 fileUpload(QString filepath);
    //文件下载
    qint64 filesDownload(QStringList filepath_list);
    qint64 fileDownload(QString filepath);

protected:
    //文件上传
    qint64 fileUploadHeader(QString filepath);
    qint64 fileUploadPos();
    qint64 fileUploadContent();
    qint64 fileUploadTail();
    //文件下载
    qint64 fileDownloadHeader(QString filepath);
    qint64 fileDownloadContent();
    qint64 fileDownloadTail();

    qint64 fileUploadRecvData(qint64 req_id,QMap<QString, QVariant>& recv_data);
    bool socketConnect();
    void paramInit();
    qint64 file_clc_md5(QString &md5_value);

private:
    QString m_ip;
    quint16 m_port;

    qint64 m_file_pos;     //文件偏移量
    qint64 m_payloadSize;   //上传的速度
    qint64 m_file_total_size;   //上传文件的总大小

    QFile m_local_file;
    QTcpSocket m_socket;
};

#endif // FILECLIENT_H
